/**
 * Created by zhengchuan on 2017/6/5.
 */
$(function(){
    var uuid = guid1()
    var loginQRCodeText =  serverPath+'?uuid='+uuid


    var generQrCode = function  (text) {
        $("#code").html("")
        $("#code").qrcode({
            render: "canvas", //table方式
            width: 150, //宽度
            height:150, //高度
            text: text //任意内容
        });
    }

    generQrCode(loginQRCodeText)

    var intervalGetLoginState = function () {
        $.ajax(
            {
                url: context_Path+'/supermarket/getQRCodeLoginResult',
                type: 'post',
                data : {
                    randNumber : uuid,
                },
                dataType : 'json',
                success: function (data) {
                    if (data.data) {
                        $("#shopIdInPut").val(data.data.shopId)
                        $("#userIdInPut").val(data.data.userId)
                        $("#goToHome").submit()
                    }
                },
                error: function (error) {

                }
            }
        );
    }

    setInterval(intervalGetLoginState, 10000);
})

var sendVerifiCode = function () {
    layer.msg('暂时不支持该方式登陆，请扫码登陆')
    return;
    var phone = $('#phoneInput').val()
    if (phone == '') {
        layer.msg('请输入手机号')
        $('#phoneInput').focus();
        return
    }
    var tel = /^1[34578]\d{9}$/;
    if (!tel.test(phone)) {
        layer.msg('手机号格式不对')
        $('#phoneInput').focus();
        return
    }
    layer.load()
    $.ajax(
        {
            url: context_Path + '/supermarket/httpPost',
            type: 'post',
            dataType: 'json',
            data: {
                url: sendVrifiCodeUrl,
                stringMap: {
                    mobile : phone,
                    type : 'login',
                    sign : 'C3C6EFA8C38B490092F112E3AB4BFE16'
                }
            },
            success: function (data) {
                layer.closeAll('loading')
                //console.log(data)
                if (data.code != '000'){
                    layer.msg(data.msg)
                    return
                }
                $ths = $('#getVerifiCodeBtn')
                $ths.attr('onclick','javascript:layer.msg("验证码已发送，请查看手机")');
                var second = 60;
                $ths.html(second+'秒后可获取')
                var dingshi = setInterval(function () {
                    if (second <= 1) {
                        $ths.html('获取验证码')
                        $ths.attr('onclick','sendVerifiCode()');
                        clearInterval(dingshi)
                        return
                    }
                    second = second -1;
                    $ths.html(second+'秒后可再获取')
                }, 1000);
                layer.msg('验证码发送成功');
            },
            error: function (error) {
                layer.closeAll('loading')
                layer.msg('网络错误')
            }
        }
    );
}

var submitLogin = function () {
    layer.msg('暂时不支持该方式登陆，请扫码登陆')
    return;
    var $phone = $('#phoneInput');
    var phone = $phone.val()
    if (phone == '') {
        layer.msg('请输入手机号')
        $phone.focus();
        return
    }
    var tel = /^1[34578]\d{9}$/;
    if (!tel.test(phone)) {
        layer.msg('手机号格式不对')
        $phone.focus();
        return
    }

    var $verifiCode = $('#verifiCodeInput');
    var verifiCode = $verifiCode.val();
    if (verifiCode == ''){
        layer.msg('请输入验证码')
        $verifiCode.focus();
        return
    }

    var veri = /^\d{6}$/;
    if (!veri.test(verifiCode)) {
        layer.msg('验证码格式不对')
        $verifiCode.focus();
        return
    }
    layer.load()
    $.ajax({
        url: context_Path + '/supermarket/httpPost',
        type: 'post',
        dataType: 'json',
        data: {
            url: userLoginUrl,
            stringMap: {
                u_type: 'M',
                u_name:phone,
                password:verifiCode,
            }
        },
        success: function (data) {

            //console.log(data)
            if (data.code != '000') {
                layer.closeAll('loading')
                layer.msg(data.msg)
                return
            }

            //登陆成功。。。
            $.ajax(
                {
                    url: context_Path + '/supermarket/httpPost',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        url: getShopsByUserId,
                        stringMap: {
                            userId : data.data.userId,
                        }
                    },
                    success: function (data1) {
                        layer.closeAll('loading')
                        //console.log(data1)
                        if (data1.code != '000'){
                            layer.msg(data1.msg)
                            return
                        }
                        //选择店铺。。。。。

                    },
                    error: function (error) {
                        layer.closeAll('loading')
                        layer.msg('网络错误')
                    }
                }
            );

        }
        ,
        error: function (error) {
            layer.closeAll('loading')
            layer.msg('网络错误')
        }
    })
}